import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 这个脚本主要用于执行部分基础sql，被Opengauss_Function_Guc_Connectionauthentication_Case0327调用
 * @Owner ：lwx1133724
 * @Create At： 2024/10/11
 */
public class Guc_connectionauthentication_Case0327 {
    public static void main(String[] args) throws Exception {
        if (args.length < 4) {
            System.out.println("need para: host:ip,dbname,user,password");
            return;
        }

        Connection conn = DriverManager.getConnection(
                "jdbc:postgresql://" + args[0] + "/" + args[1], args[2], args[3]);
        Statement stmt = null;

        try {
            stmt = conn.createStatement();

            String[] sql_list = {
                "set idle_in_transaction_session_timeout to 30;\n",
                "set session_timeout to 0;\n",
                "show idle_in_transaction_session_timeout;\n",
                "show session_timeout;\n",
                "select now();\n"
            };

            for (String sql : sql_list) {
                if (sql.trim().startsWith("select")) {
                    ResultSet resultSet = stmt.executeQuery(sql.trim());
                    while (resultSet.next()) {
                        System.out.println("Result: " + resultSet.getString(1));
                    }
                    resultSet.close();
                } else {
                    stmt.execute(sql.trim());
                }
            }

            Thread.sleep(30000);

            ResultSet resultSet = stmt.executeQuery("select now();");
            while (resultSet.next()) {
                System.out.println("Result: " + resultSet.getString(1));
            }
            resultSet.close();

            Thread.sleep(30000);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
    }
}
